<?php

namespace App\Controller\Admin;

use App\Model\AuthGroup;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Psr\Http\Message\ResponseInterface;

/**
 * @Controller(prefix="/admin/authGroup")
 */
class AuthGroupController extends AdminBaseController
{

    /**
     * @RequestMapping(path="groupList")
     * @return void
     */
    public function groupList()
    {

        $list = AuthGroup::getAllList();

        return $this->response->adminSuccess(['list' => $list]);
    }


    /**
     * 新增角色组
     * @RequestMapping(path="addGroup")
     * @return ResponseInterface
     */
    public function addGroup()
    {
        $params = $this->request->post();

        $pid = $params['pid'] ?? 0;


        if ($pid > 0) {

            $pInfo = AuthGroup::where(['id' => $pid])->first();
            if (is_null($pInfo)) {
                return $this->response->adminFail('没有找到上级：' . $pid);
            }

        }

        $where = [
            ['name', '=', $params['name']],
        ];
        $exists = AuthGroup::where($where)->exists();

        if ($exists) {
            return $this->response->adminFail('名称已经存在请勿重复添加');
        }

        $data = [
            'pid' => $params['pid'] ?? 0,
            'name' => $params['name'] ?? '管理员',
            'title' => $params['title'] ?? '管理员',
            'status' => $params['status'] ?? 1,
            'power' => $params['power'] ?? '',
        ];

        $rel = AuthGroup::insert($data);

        if ($rel) {
            return $this->response->adminSuccess();
        } else {
            return $this->response->adminFail();
        }


    }


    /**
     * @RequestMapping(path="editGroup")
     * @return ResponseInterface
     */
    public function editGroup()
    {
        $params = $this->request->post();

        $pid = $params['pid'] ?? 0;
        $id = $params['id'];

        if ($pid == $id) {
            return $this->response->adminFail('角色组关系错误');
        }
        if ($pid > 0) {

            $pInfo = AuthGroup::where(['id' => $pid])->first();
            if (is_null($pInfo)) {
                return $this->response->adminFail('没有找到上级：' . $pid);
            }

        }

        $where = [
            ['name', '=', $params['name']],
        ];
        $exists = AuthGroup::where($where)->exists();

        if ($exists) {
            return $this->response->adminFail('名称已经存在请勿重复添加');
        }

        $data = [
            'pid' => $params['pid'] ?? 0,
            'name' => $params['name'] ?? '管理员',
            'title' => $params['title'] ?? '管理员',
            'status' => $params['status'] ?? 1,
            'power' => $params['power'] ?? '',
        ];

        $rel = AuthGroup::where(['id' => $id])->update($data);

        if ($rel) {
            return $this->response->adminSuccess();
        } else {
            return $this->response->adminFail();
        }
    }

    /**
     * 改变用户组状态
     * @RequestMapping(path="changeStatus")
     * @return ResponseInterface
     */
    public function changeStatus()
    {
        $params = $this->request->post();

        $status = $params['status'] ?? 0;
        $id = $params['id'];


        $date = date('Y-m-d H:i:s');
        $rel = AuthGroup::where(['id' => $id])->update(['status' => $status,'update_time'=>$date]);

        if ($rel) {
            return $this->response->adminSuccess();
        } else {
            return $this->response->adminFail();
        }


    }


}